package com.chuangke.common.express.function.string;

import java.util.Map;

import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.function.FunctionUtils;
import com.googlecode.aviator.runtime.type.AviatorObject;
import com.googlecode.aviator.runtime.type.AviatorString;

import cn.hutool.core.util.StrUtil;

public class StringZeroPaddingBeforeFunction extends AbstractFunction {
	
	
    private static final long serialVersionUID = 7619945130938678009L;

	@Override
	public AviatorObject call(Map<String, Object> env, AviatorObject length) {
		return call(env,length,new AviatorString("0"));
	}

	@Override
	public AviatorObject call(Map<String, Object> env, AviatorObject str , AviatorObject length) {
		int sLength = FunctionUtils.getNumberValue(length, env).intValue() ;
		String sstr = FunctionUtils.getStringValue(str, env) ;
		
		return new AviatorString(StrUtil.fillBefore(sstr, '0', sLength));
	}

	@Override
	public String getName() {
		return "string.zpb";
	}
}
